package com.wh.jdbc;

import java.sql.*;

public class Jdbc {

    public static void main(String[] args) throws Exception {

        //加在驱动
        Class.forName("com.mysql.jdbc.Driver");

        //用户信息和url
        String username="root";
        String password="123456";
        String url="jdbc:mysql://localhost:3306/book" +
                "?useUnicode=true&characterEncoding=utf8&useSSL=true";

        //连接数据库,connection就代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //创建执行SQL的对象statement
        Statement statement = connection.createStatement();
        String sql="SELECT * FROM t_book WHERE id=1";
        ResultSet resultSet = statement.executeQuery(sql);
        while(resultSet.next()){
            System.out.println(resultSet.getObject("name"));
            System.out.println(resultSet.getObject("author"));
            System.out.println(resultSet.getObject("price"));
            System.out.println(resultSet.getObject("sales"));
            System.out.println(resultSet.getObject("stock"));
        }

        //释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
